package com.example.demo.controller;

import com.example.demo.entity.ApiResponse;
import com.example.demo.entity.Categories;
import com.example.demo.service.CategoriesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CategoryController {
    private final static Logger loger = LoggerFactory.getLogger(CategoryController.class);
    @Autowired
    private CategoriesService categoriesService;

    @PreAuthorize("hasAuthority('category:read')")
    @GetMapping("/categories")
    public ResponseEntity<ApiResponse> getList() {
        ApiResponse apiResponse;
        List<Categories> list = null;
        try {
            list = categoriesService.getList();
            if (list.size() > 0) {
                apiResponse = new ApiResponse(1, "数据查询成功", list);
            } else {
                apiResponse = new ApiResponse(0, "数据为空", null);
            }
            loger.info("数据：{}", list);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            apiResponse = new ApiResponse(2, "数据查询失败", null);
            loger.info("数据：{}", list);
            loger.error("数据查询失败：{}", e.getMessage());
            return ResponseEntity.ok(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('category:create')")
    @PostMapping("/categories")
    public ResponseEntity<ApiResponse> addCategories(@RequestBody Categories categories) {
        ApiResponse apiResponse;
        try {
            int line = categoriesService.addCategories(categories);
            if (line > 0) {
                apiResponse = new ApiResponse(1, "数据添加成功", null);
            } else {
                apiResponse = new ApiResponse(0, "数据添加失败", null);
            }
            loger.info("数据：{}", categories);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            apiResponse = new ApiResponse(2, "数据添加失败", null);
            loger.info("数据：{}", categories);
            loger.error("数据添加失败：{}", e.getMessage());
            return ResponseEntity.ok(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('category:update')")
    @PutMapping("/categories/{id}")
    public ResponseEntity<ApiResponse> updateCategories(@PathVariable("id") Integer id, @RequestBody Categories categories) {
        ApiResponse apiResponse;
        try {
            categories.setId(id);
            int line = categoriesService.updateCategories(categories);
            if (line > 0) {
                apiResponse = new ApiResponse(1, "数据更新成功", null);
            } else {
                apiResponse = new ApiResponse(0, "数据更新失败", null);
            }
            loger.info("id:{}数据：{}", id, categories);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            apiResponse = new ApiResponse(2, "数据更新失败", null);
            loger.info("id:{}数据：{}", id, categories);
            loger.error("数据更新失败：{}", e.getMessage());
            return ResponseEntity.ok(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('category:delete')")
    @DeleteMapping("/categories/{id}")
    public ResponseEntity<ApiResponse> deleteCategories(@PathVariable("id") Integer id) {
        ApiResponse apiResponse;
        try {
            int line = categoriesService.delCategories(id);
            if (line > 0) {
                apiResponse = new ApiResponse(1, "数据删除成功", null);
            } else {
                apiResponse = new ApiResponse(0, "数据删除失败", null);
            }
            loger.info("id:{}数据删除成功", id);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            apiResponse = new ApiResponse(2, "数据删除失败", null);
            loger.info("id:{}数据删除失败", id);
            loger.error("数据删除失败：{}", e.getMessage());
            return ResponseEntity.ok(apiResponse);
        }
    }
}
